<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\User;
use think\exception\ValidateException;
use think\Response;
use WeMini\Crypt;
use app\common\tool\Result;
use app\common\facade\WeChatMini;

class Login extends Base
{

    /**
     * 原点 2022年2月16日14:16:31 小程序授权登录
     */
    //手机号授权登录
     public function miniAppletLogin()
     {
         try {
 
            $param = $this->request->post();
            $this->validate($param, 'MiniAppletLogin');
            
            $phone=WeChatMini::getUserInfo($param['code'],$param['iv'],$param['encryptedData']);
            if(!empty($phone)){
                $param['mobile']=$phone['phoneNumber'];
                $param['openid']=$phone['openid'];
                return Result::fastReturn(User::_miniAppletSave($param));
            }else{
                return Result::fail('请重新获取授权！');
            }
         } catch (ValidateException $e) {
             return Result::fail($e->getError());
         } catch (\Exception $e) {
             return Result::fail($e->getMessage());
         }
     }
}
